package jtmcraft.game.units;

import java.util.ArrayList;

/**
 *
 * @author milosz
 */
public class CollisionManager {

    private ArrayList<CollisionObserver> observers = new ArrayList<CollisionObserver>();

    private static CollisionManager instance = null;

    private CollisionManager() {
    }

    synchronized public static CollisionManager getInstance() {
        if( null == instance ) {
            instance = new CollisionManager();
        }
        return instance;
    }

    public void registerObserver( CollisionObserver observer ) {
        observers.add( observer );
    }

    public void unregisterObserver( CollisionObserver observer ) {
        observers.remove( observer );
    }

    public GameUnit checkCollision( int x, int y, GameUnit gameUnit ) {
        for( CollisionObserver observer : observers ) {
            if( observer != gameUnit && observer.checkCollision( x, y, gameUnit ) ) {
                return (GameUnit) observer;
            }
        }
        return null;
    }
    
  


}